package com.inspur.sc.springboot.service.impl;

import com.inspur.sc.springboot.convert.SysUserConvertSysUserVO;
import com.inspur.sc.springboot.dao.SysRoleDao;
import com.inspur.sc.springboot.dao.SysRoleUserDao;
import com.inspur.sc.springboot.dao.SysUserDao;
import com.inspur.sc.springboot.entity.SysRole;
import com.inspur.sc.springboot.entity.SysRoleUser;
import com.inspur.sc.springboot.entity.SysUser;
import com.inspur.sc.springboot.service.SysUserService;
import com.inspur.sc.springboot.vo.SysUserVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserDao userDao;

    @Autowired
    private SysRoleDao roleDao;

    @Autowired
    private SysRoleUserDao roleUserDao;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public Page<SysUser> findByUsernameLike(String username, Pageable pageable) {
        if(StringUtils.isEmpty(username)){
            return userDao.findAll(pageable);
        }
        return userDao.findByUsernameLike(username,pageable);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveUser(SysUser user,List<String> roleIds) {
        try {
            userDao.save(user);
            if(null!=roleIds){
                roleUserDao.deleteBySysUserId(user.getId());
                roleIds.forEach(roleId->{
                    SysRoleUser roleUser=new SysRoleUser();
                    roleUser.setSysUserId(user.getId());
                    roleUser.setSysRoleId(Integer.valueOf(roleId));
                    roleUserDao.save(roleUser);
                });
            }
            return true;
        }catch (Exception e){
            log.error(e.getMessage());
            return false;
        }
    }

    @Override
    public SysUserVO findUserById(Integer id) {
        SysUser sysUser=userDao.findById(id).get();
        if(null!=sysUser){
            SysUserVO userVO=SysUserConvertSysUserVO.convert(sysUser);
            List<SysRole> roles=roleDao.findByUserId(id);
            List<Integer> roleIds=new ArrayList<>();
            roles.forEach(role->{
                roleIds.add(role.getId());
            });
            userVO.setRoleIds(roleIds);
            return userVO;
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteUserById(Integer id) {
        try{
            userDao.deleteById(id);
            return true;
        }catch (Exception e){
            log.error(e.getMessage());
            return false;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean restPwd(Integer id) {
        try{
            SysUser user=userDao.findById(id).get();
            user.setPassword(passwordEncoder.encode("123456"));
            userDao.save(user);
            return true;
        }catch (Exception e){
            log.error(e.getMessage());
            return false;
        }
    }
}
